//
//  FlowWaterLayout.m
//  瀑布流
//
//  Created by Crow Zhang on 15/11/9.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "FlowWaterLayout.h"

@interface FlowWaterLayout()

@end

@implementation FlowWaterLayout

#pragma mark 重写系统的prepareLayout方法 准备布局item之前会调用
- (void)prepareLayout
{
    //调用父类分方法
    [super prepareLayout];
    self.maxHeight = 0;
    
    //用来存放item的属性
    self.array = [NSMutableArray array];
    
    //第一列item y坐标的初始值
    CGFloat leftY = 10;
    
    //第二列item y坐标的初始值
    CGFloat rightY = 10;
    
    //计算contentView
    //第一步
    CGFloat maxLeft = 0;
    CGFloat maxRight = 0;
    
    //计算当前分区有多少个分区
    NSInteger c = [self.collectionView numberOfSections];
    for (int j = 0; j < c; j++) {
        NSInteger count = [self.collectionView numberOfItemsInSection:j];
        if(j == 1)
        {
            //第一列item y坐标的初始值
            leftY = ([UIScreen mainScreen].bounds.size.width  - 40) / 3 + 20;
            
            //第二列item y坐标的初始值
            rightY = ([UIScreen mainScreen].bounds.size.width  - 40) / 3 + 20;
        }
        //设置每个item的属性
        for (int i = 0; i < count; i++) {
            //生成item下表
            NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:j];
            //根据item下表取出每个item属性对象 通过它能设置item的frame
            UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
            //默认x坐标为0
            CGFloat offSetX = 0;
            
            if (j == 0) {
                
                if (i % 2 == 0) {
                    //设置第一列 item x坐标
                    offSetX = 10;
                }else
                {
                    offSetX = [UIScreen mainScreen].bounds.size.width / 2 + 5;
                }
            }else if (j == 1 && i < 3)
            {
                if (i % 2 == 0) {
                    //设置第一列 item x坐标
                    offSetX = 10;
                }else
                {
                    offSetX = ([UIScreen mainScreen].bounds.size.width - 40) / 3 + 20;
                }
            }else
            {
                
                if (i % 2 == 0) {
                    //设置第一列 item x坐标
                    offSetX = 10;
                }else
                {
                    offSetX = ([UIScreen mainScreen].bounds.size.width - 40) / 3 * 2 + 30;
                }
            }
            
            
            //获取协议 返回的size
            CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:index];
            
            //设置每个item的属性
            if (i % 2 == 0) {
                //设置第一列 item
                attributes.frame = CGRectMake(offSetX, leftY, itemSize.width, itemSize.height);
                //记录左侧所有item高度
                maxLeft = leftY + itemSize.height;
                leftY += itemSize.height + 10;
            }else
            {
                attributes.frame = CGRectMake(offSetX, rightY, itemSize.width, itemSize.height);
                maxRight = rightY + itemSize.height;
                rightY += itemSize.height + 10;
            }
            self.maxHeight = maxLeft > maxRight ? maxLeft + 10 : maxRight + 10;
            [self.array addObject:attributes];
        }

    }
}

// 重写系统方法 初始化layout的外观由该方法的UIcollectionViewLayoutAttributes来决定

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < self.array.count; i++) {
        UICollectionViewLayoutAttributes *att = [self.array objectAtIndex:i];
        //判断第二个rect是不是在第一个rect范围之内
        if (CGRectIntersectsRect(rect, att.frame)) {
            [array addObject:att];
        }
    }
    return array;
}

//重新设置滑动范围
- (CGSize)collectionViewContentSize{
    return CGSizeMake(self.collectionView.frame.size.width, self.maxHeight);
}

@end
